<?php

declare(strict_types=1);

namespace App\Service;

/**
 * 商家认证信息服务
 * Class FlyShopCertificateService
 * @package App\Service
 */
class FlyShopCertificateService extends BaseService
{
    const TYPE_FIGURES = 1; // 门店信息
    const TYPE_LICENSE = 2;// 营业执照
    const TYPE_ID = 3; // 身份证
    const TYPE_FOOD_LICENSE = 5;// 食品经营许可证

    /**
     * 保存认证基本信息
     * @param $params
     * @param bool $return_id
     * @return bool | int
     */
    public function saveCert($params, bool $return_id = true)
    {
        $cert_fields = ['shop_id', 'fullname', 'cert_number', 'cert_type', 'create_time', 'user_id'];

        $model = $this->getModelInstance();
        $now = time();
        foreach ($cert_fields as $cert_field) {
            if (!isset($cert_field)) {
                return false;
            }
            if (in_array($cert_field, ['create_time'])) {
                $model->$cert_field = $now;
            } else {
                $model->$cert_field = $params[$cert_field];
            }
        }

        if (!$model->save()) {
            return false;
        }
        return $return_id ? $model->id : true;
    }
}